﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VirtualHost.WinForm {
    public partial class FrmEditHosts : WinFormBase {
        private String FileName { get; }

        public FrmEditHosts() {
            InitializeComponent();

            String value = Environment.GetEnvironmentVariable("SystemRoot");
            FileName = String.Format(@"{0}\\System32\drivers\etc\hosts", value);

            LoadHost();
        }

        private void LoadHost() {
            String contents = File.ReadAllText(FileName);
            txtContents.Text = contents;
            //Console.WriteLine(value);
        }
        private void btnSave_Click(object sender, EventArgs e) {
            try {

                String contents = txtContents.Text;
                File.WriteAllText(FileName, contents);
                ShowMessage("数据保存成功！");
            } catch (Exception ex) {
                ShowMessage(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        private void ShowMessage(String msg) {
            lblMsg.Text = msg;
        }
    }
}
